package lv.solcraft.replication.server.bdb;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.util.DbVerify;

@Service
public class DbMaintenance {

    private final Database database;

    @Autowired
    public DbMaintenance(Database database) {
        this.database = database;

    }

    public List<String> verify() throws DatabaseException {
        PrintStream ps = new PrintStream(System.out);
        List<String> errorDbs = new ArrayList<String>();
        for (String dbName : database.getEnvironment().getDatabaseNames()) {
            System.out.println("Verifying: " + dbName);
            DbVerify verify = new DbVerify(database.getEnvironment(), dbName, false);
            if (!verify.verify(ps)) {
                errorDbs.add(dbName);
            }
        }
        return errorDbs;
    }
}
